package com.liuya.common.support.spring.data.redis;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import org.springframework.util.Assert;

public class MyJdkSerializationRedisSerializer implements RedisSerializer<Object> {

	private final Converter<Object, byte[]> serializer;
	private final Converter<byte[], Object> deserializer;

	public MyJdkSerializationRedisSerializer() {
		this(new MySerializingConverter(), new MyDeserializingConverter());
	}

	public MyJdkSerializationRedisSerializer(ClassLoader classLoader) {
		this(new MySerializingConverter(), new MyDeserializingConverter(classLoader));
	}

	public MyJdkSerializationRedisSerializer(Converter<Object, byte[]> serializer, Converter<byte[], Object> deserializer) {

		Assert.notNull("Serializer must not be null!");
		Assert.notNull("Deserializer must not be null!");

		this.serializer = serializer;
		this.deserializer = deserializer;
	}

	boolean isEmpty(byte[] data) {
		return (data == null || data.length == 0);
	}

	public Object deserialize(byte[] bytes) {
		if (isEmpty(bytes)) {
			return null;
		}

		try {
			return deserializer.convert(bytes);
		} catch (Exception ex) {
			throw new SerializationException("Cannot deserialize", ex);
		}
	}

	public byte[] serialize(Object object) {
		if (object == null) {
			return new byte[0];
		}
		try {
			return serializer.convert(object);
		} catch (Exception ex) {
			throw new SerializationException("Cannot serialize", ex);
		}
	}
}
